package cn.edu.cqupt.cxcy.filter;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;



/**
 * 过滤非空或有空格的修饰类
 * @author Dancer-mno
 * @date 2015年10月11日
 */
public class OpRequestWrap extends HttpServletRequestWrapper{

	public OpRequestWrap(HttpServletRequest request) {
		super(request);
	}

	private String format(String name) {
//		return StringUtils.replaceEach(name,new String[]{"\"","'","<",">"},new String[]{"\"","′","<",">"});
		return null;
	}

	/**
	 *
	 * @param name
	 * @return
	 */
	public Object getAttribute(String name) {
		Object value = super.getAttribute(name);
		if (value instanceof String) {
			value = format(String.valueOf(value));
		}
		return value;
	}

	/**
	 * 重写getParameter方法
	 *
	 * @param name
	 * @return
	 */
	public String getParameter(String name) {
		String value = super.getParameter(name);
		if (value == null)
			return null;
		return format(value);
	}

	/**
	 *
	 * @param name
	 * @return
	 */
	public String[] getParameterValues(String name) {
		String[] values = super.getParameterValues(name);
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				values[i] = format(values[i]);
			}
		}
		return values;
	}

	/**
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map getParameterMap() {

		HashMap paramMap = (HashMap) super.getParameterMap();
		paramMap = (HashMap) paramMap.clone();

		for (Iterator iterator = paramMap.entrySet().iterator(); iterator.hasNext(); ) {
			Map.Entry entry = (Map.Entry) iterator.next();
			String [] values = (String[]) entry.getValue();
			for (int i = 0; i < values.length; i++) {
				if(values[i] instanceof String){
					values[i] = format(values[i]);
				}
			}
			entry.setValue(values);
		}
		return paramMap;
	}

}
